package middle;

import util.PrintUtils;

public class Solution_238 {

    public static void main(String[] args) {
        Solution_238 model = new Solution_238();
        PrintUtils.arrayInt(model.productExceptSelf(new int[]{1, 2, 3, 4}));
        PrintUtils.arrayInt(model.productExceptSelf(new int[]{-1, 1, 0, -3, 3}));
    }

    public int[] productExceptSelf(int[] nums) {
        int pre = 1, next = 1;
        int[] arrays = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            arrays[i] = pre;
            pre *= nums[i];
        }
        for (int i = nums.length - 1; i >= 0; i--) {
            arrays[i] *= next;
            next *= nums[i];
        }
        return arrays;
    }

    public int[] productExceptSelf1(int[] nums) {
        int pre = 1;
        int[] arrays = new int[nums.length];
        arrays[arrays.length - 1] = 1;
        for (int i = nums.length - 2; i >= 0; i--) {
            arrays[i] = nums[i + 1] * arrays[i + 1];
        }
        int[] rst = new int[nums.length];
        for (int i = 0; i < rst.length; i++) {
            rst[i] = pre * arrays[i];
            pre *= nums[i];
        }
        return rst;
    }
}
